<ul class="info-item-block">
    <li>
        <span class="item-hd item-title">维护价格库存</span>
    </li>
    <li>
        <div class="item-bd alone-item-bd">
            <div>
                <div id="calendar_price_suit" class="w300 fl mr-10"></div>
                <span class="btn btn-primary btn-radius" id="edit_price_stock">设置价格/库存</span>
                <span class="btn btn-primary-outline btn-radius" id="del_price">清除报价</span>
<!--                <span class="btn btn-primary-outline btn-radius">清除库存</span>-->
                <input type="hidden" name="calendar_price_date" value="{date('Y-m-d')}">
            </div>
            <div class="calendar-container clearfix" id="calendarPrice"></div>
        </div>
    </li>
</ul>
<script type="application/javascript">
    var calendarPriceSuitSelectTicketId = [],
        calendarPriceSuitSelectSkuId = {};
    // 价格日历筛选初始化
    var calendarPriceSuitSelect = xmSelect.render({
        el: '#calendar_price_suit',
        size: 'mini',
        name: 'price_stock_sku',
        height: '200px',
        tips: '请选择套餐',
        toolbar: {
            show: true,
            showIcon: false,
        },
        prop: {
            name: 'title',
            value: 'tag',
        },
        filterable: true,
        theme: {
            color: '{Common::get_back_color()}',
        },
        data: function () {
            return {json_encode($skus)}
        },
        on: function (data) {
            var arr = data.arr,ticketId = [],skuId = {}
            calendarPriceSuitSelect.closed();

            $.each(arr, function(index, item) {
                var currentTicketId = item.ticket_id;
                if (ticketId.indexOf(currentTicketId) === -1) {
                    ticketId = ticketId.concat(currentTicketId);
                    skuId[currentTicketId] = [item.sku_id];
                } else {
                    skuId[currentTicketId] = skuId[currentTicketId].concat(item.sku_id);
                }
            })

            calendarPriceSuitSelectTicketId = ticketId
            calendarPriceSuitSelectSkuId = skuId

            getSuitPriceCalendar()
        }
    });


    changePriceSuitSku()

    function changePriceSuitSku() {
        var spotId = $("input[name=productid]").val();
        if(!spotId) {
            return false
        }
        $.post(SITEURL + "spot/admin/spot/ajax_get_price_suit_sku", {spot_id: spotId}, function (res) {
            if (res.status) {
                calendarPriceSuitSelect.update({
                    data: res.data
                })
            } else {
                calendarPriceSuitSelect.update({
                    data: []
                })
            }
        }, 'json')

        // 获取套餐价格日历
        getSuitPriceCalendar();
    }

    // 初始化价格日历视图
    var calendar = $('#calendarPrice').calendarPrice({
        type: 1,
        onShow: function (data) {
            var title = "修改库存/报价（"+data.date+" | "+data.title+"）",
                queryStr = 'spot_id={$info["id"]}&ticket_id='+data.ticket_id+'&sku_id='+data.sku_id+'&date='+data.date;
            ST.Util.showBox(title, SITEURL + 'spot/admin/spot/dialog_edit_price?'+queryStr, 1200, 300, null, null, document, {
                loadWindow: window,
                loadCallback: function () {
                    getSuitPriceCalendar();
                }
            })
        },
        onChangeDate: function (date) {
            $("input[name=calendar_price_date]").val(date);

            getSuitPriceCalendar();
        }
    });

    // 获取套餐价格日历
    function getSuitPriceCalendar() {
        var spot_id = $("input[name=productid]").val();
        if(!spot_id) {
            return false
        }

        $(".calendar-container").addClass('loading');
        var startDate = $("input[name=calendar_price_date]").val(),
            postData = {spot_id},
            dayNum = 30;

        postData.start_date = startDate;
        postData.day_num = dayNum;
        postData.ticket_id = calendarPriceSuitSelectTicketId;
        postData.sku_id = calendarPriceSuitSelectSkuId;
        $.post(SITEURL + "spot/admin/spot/ajax_get_price_calendar", postData, function (res) {
            calendar.setStartDate(startDate)
            calendar.createElement()

            $(".calendar-container").removeClass('loading')
            calendar.setData(res.data);

        }, 'json')
    }

    // 价格库存编辑
    $("#edit_price_stock").on('click', function(e){
        ST.Util.showBox('设置价格/库存', SITEURL + 'spot/admin/spot/dialog_edit_price/action/all?spot_id={$info["id"]}', 1000, 600, null, null, document, {
            loadWindow: window,
            loadCallback: function () {
                getSuitPriceCalendar();
            }
        })
    })

    // 清除报价
    $("#del_price").on('click', function (event) {
        var spot_id = $("input[name=productid]").val();
        if(!spot_id) {
            return false
        }
        var skuIds = $("input[name=price_stock_sku]").val(),
            content = skuIds ? '是否清除指定套餐的报价' : '是否清除全部报价';
        ST.Util.confirmBox('操作确认', content + '，报价清除后无法找回？', function () {
            ST.Util.showLoading('保存中');
            $.post(SITEURL + "spot/admin/spot/ajax_clear_price", {spot_id, sku_id: skuIds}, function (res) {
                if (res.status) {
                    ST.Util.showSuccessMsg(res.msg)
                    getSuitPriceCalendar();
                } else {
                    ST.Util.showFailureMsg(res.msg)
                }
            }, 'json')
        })
    })
</script>